package org.psneog.jdbc;
import java.sql.Connection;

import org.apache.derby.jdbc.Driver40;
import org.apache.derby.jdbc.EmbeddedDriver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DerbyTest {
  private Connection connect = null;
  private Statement statement = null;
  private ResultSet resultSet = null;
  private String driver = "org.apache.derby.jdbc.EmbeddedDriver";

  public DerbyTest() throws Exception {
    try {
      Class.forName(driver).newInstance();
    } catch (ClassNotFoundException cnfe) {
    	cnfe.printStackTrace();
    	System.exit(-1);
    }
    try {
      connect = DriverManager.getConnection("jdbc:derby:C:/Users/Prabhakar/MyDB;create=true");
    } catch (SQLException sqe1) {
    	sqe1.printStackTrace();
    	System.exit(-1);
    }
    try {	
      PreparedStatement statement = connect.prepareStatement("SELECT * from USERS");
      resultSet = statement.executeQuery();
    } catch (SQLException sqe2) {
    	sqe2.printStackTrace();
    	System.exit(-1);
    }
    try {
      while (resultSet.next()) {
        String user = resultSet.getString("name");
        String number = resultSet.getString("number");
        System.out.println("User: " + user);
        System.out.println("ID: " + number);
      }
    } catch (Exception e) {
      throw e;
    } finally {
      close();
    }

  }

  private void close() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }
      if (statement != null) {
        statement.close();
      }
      if (connect != null) {
        connect.close();
      }
    } catch (Exception e) {

    }
  }

  public static void main(String[] args) throws Exception {
    DerbyTest dao = new DerbyTest();
  }

} 